(*filename: inter.ml*)

let adkasdjka = GMain.init ()

let currentStep = ref 0 
let traitHasSet = ref false
let pas = ref 10

  (*permet de savoir si l'on est en pretraitement ou en traitement ect..*)
let changeStep stp = 
  if (stp = "Contour") then
    currentStep :=  1
  else if (stp = "Cadrillage") then
    currentStep := 2
  else if (stp = "TraitColor") then
    currentStep:=3
  else
    currentStep := 0 

(*fenetre principale*)
let window =
  let wnd = GWindow.window
  ~title:"Titre"
  ~height:600
  ~width:800
  ~resizable:true
  ~border_width:10 () in
  wnd#unfullscreen ();
  wnd#connect#destroy GMain.quit;
wnd
(******************************************************************************)

(*creation de trois onglets*)
(*insertion de la tabble dans la fenetre principale*)
let table =
  let tbl = GPack.table
    ~rows:3
    ~columns:3
    ~packing:window#add () in 
  tbl

(*incertion de notebook dans la table*)
let notebook = 
  let ntb = GPack.notebook
    ~tab_pos:`TOP
    ~packing:(table#attach ~left:0
	                   ~right:6
	                   ~top:1
			   ~expand: `BOTH) () in
  ntb
 
(*creation des trois onglets, avec leur frame)*)

(*contenu de l'onglet 2D*)

 let label =
   let lbl = GMisc.label
      ~text:("Traitement 2D") () in
    lbl

 let boxPage1 =
   let hbox = GPack.hbox
     ~border_width:10
     ~packing:(fun o -> ignore(notebook#append_page
				 ~tab_label:label#coerce o)) () in
   hbox
 
 let frame1 =
    let frm = GBin.frame
      ~label:"Image 2D"
      ~width:40
      ~height:350
      ~border_width:5
      ~packing:(boxPage1#add) () in
    frm

 let tableProcess =
   let table = GPack.table
     ~rows:3 
     ~row_spacings:5
    (* ~border_width:5*)
     ~packing:(frame1#add) () in
   table

let buttonPret =
  let button = GButton.toggle_button
    ~label:"Pretraitement"
    ~active:false
    ~relief:`NORMAL
    ~packing:(tableProcess#attach ~left:0 ~top:1  ~ypadding:2 ) () in
   button


(*emplacement pour le pitchButton (configuration du pas)*)
let frameConfigue =
  let frmConf = GBin.frame
    ~label: "Pas Configue"
    ~packing:(tableProcess#attach ~left:0 ~top:2 ~ypadding:2 ) () in
  frmConf

let boxConfigue =
  let vbox = GPack.vbox 
    ~border_width:5
    ~packing:frameConfigue#add () in
  vbox

let vboxPas = 
  let boxpas = GPack.vbox
    ~packing:boxConfigue#add () in
  boxpas
                   (*********************)

(*un spinner pour le pas, son label,son parametrage*)

let label =
  let lbl = GMisc.label 
    ~text:"Pas :" 
    ~xalign:0.0 
    ~yalign:0.5 
    ~packing:vboxPas#add () in
  lbl

let adj = 
  let dj =GData.adjustment 
    ~value:10.0 
    ~lower:1.0 
    ~upper:100.0 
    ~step_incr:1.0 
    ~page_incr:5.0 
    ~page_size:0.0 () in
  dj

let spinner = 
  let spin = GEdit.spin_button 
    ~adjustment:adj 
    ~rate:0.0 
    ~digits:0 
    ~wrap:true 
    ~packing:vboxPas#add () in
  spin#misc#hide ();
  spin
                   (************************)

(*creation bouttons permetant d'avoir un visu de chaque etape du pretraitement*)

let pasApply =
  let pas = GButton.check_button
    ~label:"apply"
    ~active:false
    ~packing:vboxPas#add () in
  pas#misc#hide ();
  pas

let setPas =  (fun () -> pas := spinner#value_as_int)

(*permet de cacher/montrer les configue du pas*)
let hidePasConfigue () = 
  pasApply#misc#hide ();
  spinner#misc#hide ()

let showPasConfigue () =
  pasApply#misc#show ();
  spinner#misc#show ()

(******************************************************************************)

(*zone de configuration pour le bouton traitement*)

(*definition des reference pour les couleurs qui serons present sur la map*)
let color0 = ref (`RGB (10,10,10))

let color1 = ref (`RGB (10,10,10))

let color2 = ref (`RGB (10,10,10)) 



    (***********)


(*frame contenant les couleur et les spinner pour regler l'alt.*)
let frameAltColor =
  let frmConf = GBin.frame
    ~label: "Color configue"
    ~packing:(tableProcess#attach ~left:0 ~top:4) () in
  frmConf
    (***********)

(*table contenant les couleur present sur la map ainsi que les
  spinner pour regler l'altitude*)
let tableColorConfigue =
  let tbl = GPack.table
    ~rows:4
    ~columns:4
    ~border_width:5
    ~row_spacings:5
    ~col_spacings:5
    ~packing:frameAltColor#add () in 
  tbl
    (***********)
let label =
  let lbl = GMisc.label
    ~text:"Color:"
    ~xalign:0.0 
    ~yalign:0.5 
    ~packing:(tableColorConfigue#attach ~left:0 ~top:0) () in
  lbl

(*zone de visu pour couleurs*)
let drawingArea0 = 
  let drArea = GMisc.drawing_area
    ~width:80
    ~height:30
    ~packing:(tableColorConfigue#attach ~left:0 ~right:2 ~top:1) () in
  drArea#misc#modify_bg [`NORMAL, !color0];
  drArea#misc#hide ();
  drArea

let drawingArea1 = 
  let drArea = GMisc.drawing_area
    ~width:80
    ~height:30
    ~packing:(tableColorConfigue#attach ~left:0 ~right:2 ~top:2) () in
  drArea#misc#modify_bg [`NORMAL, !color1];
  drArea#misc#hide ();
  drArea

let drawingArea2 = 
  let drArea = GMisc.drawing_area
    ~width:80
    ~height:30
    ~packing:(tableColorConfigue#attach ~left:0 ~right:2 ~top:3) () in
  drArea#misc#modify_bg [`NORMAL, !color2];
  drArea#misc#hide ();
  drArea
    (************)

(*Spinner pour reglet l'altitude*)

(*label pour les spinner*)
let label =
  let lbl = GMisc.label 
    ~text:"Altitude :" 
    ~xalign:0.0 
    ~yalign:0.5 
    ~packing:(tableColorConfigue#attach ~left:3 ~top:0) () in
  lbl
    (************)

(*Spinner pour regler l'altitude de la premiere couleur*)
let adjAltColor0 = 
  let dj =GData.adjustment 
    ~value:0.0 
    ~lower:1.0 
    ~upper:100.0 
    ~step_incr:1.0 
    ~page_incr:5.0 
    ~page_size:0.0 () in
  dj

let spinnerAltColor0 = 
  let spin = GEdit.spin_button 
    ~adjustment:adjAltColor0 
    ~rate:0.0 
    ~digits:0 
    ~wrap:true 
    ~packing:(tableColorConfigue#attach ~left:3 ~top:1) () in
  spin#misc#hide ();
  spin
    (************)

(*Spinner pour regler l'altitude de la seconde couleur*)
let adjAltColor1 = 
  let dj =GData.adjustment 
    ~value:0.0 
    ~lower:1.0 
    ~upper:100.0 
    ~step_incr:1.0 
    ~page_incr:5.0 
    ~page_size:0.0 () in
  dj

let spinnerAltColor1 = 
  let spin = GEdit.spin_button 
    ~adjustment:adjAltColor1 
    ~rate:0.0 
    ~digits:0 
    ~wrap:true 
    ~packing:(tableColorConfigue#attach ~left:3 ~top:2) () in
  spin#misc#hide ();
  spin
    (************)

(*Spinner pour regler l'altitude de la troisieme couleur*)
let adjAltColor2 = 
  let dj =GData.adjustment 
    ~value:0.0 
    ~lower:1.0 
    ~upper:100.0 
    ~step_incr:1.0 
    ~page_incr:5.0 
    ~page_size:0.0 () in
  dj

let spinnerAltColor2 = 
  let spin = GEdit.spin_button 
    ~adjustment:adjAltColor2 
    ~rate:0.0 
    ~digits:0 
    ~wrap:true 
    ~packing:(tableColorConfigue#attach ~left:3 ~top:3) () in
  spin#misc#hide ();
  spin

(*bouton de passage a la 3D*)
(*let button3D =
  let btn = GButton.toggle_button
    ~label:"Go 3D!"
    ~packing:(tableColorConfigue#attach ~left:1  ~top:4 ~ypadding:2)
    () in
  btn#misc#hide ();
  btn
*)

(*methode pour cacher/afficher les configue lier au traitement*)
let hideColorConfigue () = 
  drawingArea0#misc#hide ();
  drawingArea1#misc#hide ();
  drawingArea2#misc#hide ();
  spinnerAltColor0#misc#hide ();
  spinnerAltColor1#misc#hide ();
  spinnerAltColor2#misc#hide ()

let showColorConfigue  ()= 
  drawingArea0#misc#show ();
  drawingArea1#misc#show ();
  drawingArea2#misc#show ();
  spinnerAltColor0#misc#show ();
  spinnerAltColor1#misc#show ();
  spinnerAltColor2#misc#show ()

(*let parsColor i = match i with
  |0 -> color0
  |1 -> color1
  |2 -> color2
  |3 -> color3
  |_ -> ()

let setColor () = 
  let listColor = Traitement.list_color "/usr/tmp/imageCadriller.bmp"
  in
  for i=0 to listColor List.lenght (listColor) do
    parsColor i := ListColor[i];
  done

let showableColor () = if color1 = color2 then
*)  

(*******************************************************************************)

let buttonTrait =
  let button = GButton.toggle_button
    ~label:"Traitement"
    ~active:false
    ~relief:`NORMAL
    ~packing:(tableProcess#attach ~left:0 ~top:3  ~ypadding:2 ) () in
  button
      

(*contenu onglet 3D*)

 let label =
   let lbl = GMisc.label
      ~text:("Traitement 3D") () in
    lbl

let frame2 =
    let frm = GBin.frame
      ~label:"3D"
      ~width:650
      ~height:350
      ~border_width:10
      ~packing:(fun o -> ignore(notebook#append_page
				 ~tab_label:label#coerce o)) () in
    frm

let label2 =
  let lbl =GMisc.label 
    ~text:"appercu 3D"
    ~packing:frame2#add () in 
  lbl



(*contenu onglet 3*)

 let label =
   let lbl = GMisc.label
      ~text:("Configuration suplementaire") () in
    lbl

let frame3 =
    let frm = GBin.frame
      ~label:"Configue"
      ~width:650
      ~height:350
      ~border_width:10
      ~packing:(fun o -> ignore(notebook#append_page
				  ~tab_label:label#coerce o)) () in
    frm
    
let label3 =
  let lbl =GMisc.label 
    ~text:"appercu Configue"
    ~packing:frame3#add () in 
  lbl



(*fenetre de recherche de fichier*)
 let fileSelector = 
   let fileSelect = GWindow.file_selection 
     ~title: "Picture selection"
     ~border_width:10
     ~parent:window
     ~destroy_with_parent:true
     ~filename: "File:" () in
   fileSelect

  (*fenetre de sauvegarde*)
 (*let fileSavor =
   let save = GWindow.file_chooser_dialog 
     ~action:`SAVE
     ~backend:""
     ~parent:window
     ~title:"Save" () in
   save#add_button_stock `CANCEL `CANCEL;
   save#add_select_button_stock `SAVE `SAVE;
   save
 *)
(******************************************************************************)
 
(*insertion de l'image dans les frames de l'onglet 1*)

  let frameImage = 
    let frmImg =  GBin.frame
      ~width:400
      ~packing:boxPage1#add () in 
    frmImg

  (*reference de chemin des fichiers*)
  let imagePath = ref fileSelector#filename
  let imagePathInit = ref !imagePath

  let imageConteneur =
    let imgCont = GMisc.image                 (*!imagePath*)
      ~file:"!imagePath"
      ~packing:frameImage#add () in 
    imgCont

  let displayImage () =  
    imageConteneur#set_file !imagePath

  let setImagePath () =  
    imagePath := fileSelector#filename;
    changeStep "Initializ";
    imageConteneur#set_file fileSelector#filename;
    fileSelector#misc#hide ()  

(******************************************************************************)


(*boutton open, ouvre une nouvelle fenetre pour selectionner une img*)
  let toolbar = 
    let bar = GButton.toolbar
      ~orientation:`HORIZONTAL
      ~style:`ICONS
      ~border_width:5
      ~packing:(table#attach ~left:0 ~right:6 ~top:0 ) () in
    bar

  let buttonOpen = 
    let button = GButton.tool_button
      ~label:"OPEN"
      ~stock:`OPEN
      ~packing:toolbar#insert () in
    button


  let separator = 
    let sep = GButton.separator_tool_item
      ~packing: toolbar#insert () in
    sep
      
  let buttonSave =
    let btn = GButton.tool_button
      ~stock:`SAVE
      ~packing:toolbar#insert () in
    btn
      
  let separator2 = 
    let sep = GButton.separator_tool_item
      ~packing: toolbar#insert () in
    sep
      
  let buttonClose =
    let btn = GButton.tool_button
      ~stock:`CLOSE
      ~packing:toolbar#insert () in
    btn
      
      
  let separator3 = 
    let sep = GButton.separator_tool_item
      ~packing: toolbar#insert () in
    sep
(****************************************************************************)
 


  let main () =
    
    fileSelector#connect#destroy (fun () -> ignore (fileSelector#run);
	fileSelector#misc#hide ());
     
    fileSelector#ok_button#connect#clicked ~callback: (setImagePath);
    
    fileSelector#cancel_button#connect#clicked 
      (fun () -> ignore (fileSelector#run);
	fileSelector#misc#hide ());
    
    buttonOpen#connect#clicked ~callback:(fileSelector#show);
    
    (*buttonSave#connect#clicked 
      ~callback:(fun () -> if (fileSavor#run () = `SAVE) then
	  Gaux.may print_endline fileSavor#filename);
    *)
    buttonClose#connect#clicked ~callback:GMain.Main.quit;
    
  


    buttonPret#connect#toggled ~callback:(fun () -> 
      if (buttonPret#active) then
	begin
	  showPasConfigue ();
	  if (!currentStep = 0)&&(!imagePath <> !imagePathInit) then
	    begin
	      Traitement.initializSdl ();
	      Traitement.saveBMP(Traitement.contour(Traitement.load_img
						      !imagePath)) "/usr/tmp/imageContour.bmp";
	      imagePath := "/usr/tmp/imageContour.bmp";
	      traitHasSet := true;
	      changeStep "Contour";
	      displayImage();
	      (*buttonTrait#set_active false*)
	    end
	  else
	    begin
	      changeStep "Initializ";
	      setImagePath();
	     (* buttonTrait#set_active false*)
	    end
	  end
      else
	begin
	  changeStep "Initializ";
	  setImagePath();
      	  hidePasConfigue ();
	  pasApply#set_active false
	end);
     


    pasApply#connect#toggled ~callback:(fun () -> 
      if (pasApply#active) then
	begin
	  pas := spinner#value_as_int;
	  Traitement.saveBMP(Traitement.cadrillage
			       (Traitement.load_img !imagePath) !pas)
	    "/usr/tmp/imageCadriller.bmp";
	  imagePath := "/usr/tmp/imageCadriller.bmp";
	  changeStep "Cadrillage";
	  displayImage();
	  buttonTrait#set_active false
	end 
      else
	begin
	  changeStep "Contour";
	  displayImage ();
	  if (!traitHasSet) then
	    begin
	  imagePath := "/usr/tmp/imageContour.bmp";
	  end
	 
	end); 

      
    buttonTrait#connect#toggled (fun () -> if (!currentStep <> 2) then
	  buttonTrait#set_active false;
      if (buttonTrait#active) then
	(*let listColor = Traitement.list_color
	  "/usr/tmp/imageCadriller.bmp" in*)
	
	      (*affichage des couleur*)
	begin
	  showColorConfigue ();
	end
      else
	begin
	  hideColorConfigue ();
	end);

    window#show ();
    GMain.main ()
